import { app, BrowserWindow, nativeImage } from "electron";
import path from "node:path";
import { MainWinMessage } from "./main/base/MainWinMessage";
import { createTray } from "./main/base/tray";
import { createMenu } from "./main/base/menu";
import registerModules from './main/modules/modules'

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
  app.quit();
}
/*
  开发环境：
  主线程运行路径 .vite/build/main.js
  public文件夹内容会放在.vite/build下
  生产环境：
  app.asar内的源代码文件(包括public转入的文件)不能修改
 */
const icon = nativeImage.createFromPath(
  path.join(__dirname, "./images/tuBiao.png")
);

const bgColor = '#048'

const createWindow = () => {
  const mainWindow = new BrowserWindow({
    width: 1280,
    height: 720,
    icon: icon,
    backgroundColor: bgColor,
    // transparent: true,
    titleBarStyle: 'hidden',
    titleBarOverlay: {
      color: bgColor,
      symbolColor: '#fff',
      height: 30 // 有最低值
    },
    webPreferences: {
      preload: path.join(__dirname, "preload.js")
    },
  });

  if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
  } else {
    mainWindow.loadFile(
      path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`)
    );
  }

  // mainWindow.on("ready-to-show", () => {
  //   mainWindow.webContents.send("ipc-message", { type: "appStart" });
  // });

  mainWindow.webContents.openDevTools();

  new MainWinMessage(mainWindow.webContents)
};

// app.on('ready', createWindow);

app.whenReady().then(() => {
  createWindow();

  createTray()
  createMenu()
  registerModules()
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});